<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :node-data="company" :is-root="true" @add-depts="handleAddDepts" />
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 主要用的是data字段，node字段仅了解，双大括号会报错
          data是当前节点的数据-->
          <template #default="{ data }">
            <!-- {{ data.name }} - {{ data.manager }} -->
            <tree-tools
              :node-data="data"
              :is-root="false"
              @del-depts="getDepartments"
              @add-depts="handleAddDepts"
              @edit-depts="handeleEditDepts"
            />
          </template>
        </el-tree>
        <add-dept
          ref="addDeptRef"
          :dialog-visible.sync="dialogVisible"
          :node-data="nodeData"
          :depts="depts"
          @close="closeDialogFn"
          @add-depts="getDepartments"
        />
      </el-card>
    </div>

  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils/index'
import treeTools from './components/tree-tools.vue'
import { reqGetDepartments } from '@/api/departments'
import AddDept from './components/add-dept.vue'
export default {
  name: 'Departments',
  /*  * 树状数据 对象数组 核心就2个字段
       *   label- 节点的名称 字符串
       *   children - 孩子节点们 对象数组
          tree组件 data指的是对象数组
          props 修改映射，因为后端的字段不一定是label和children
          default-expand-all 是否默认展开所有 默认值false 默认不展开
          完整写法 :default-expand-all="true"
          default-expand-all与上述完整写法等价
       * */
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      loading: false,
      depts: [],
      nodeData: {},
      dialogVisible: false,
      company: { },
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  async created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()
      this.company = { name: companyName, manager: '负责人', id: '' }
      // // 获取用户资料
      // console.log(JSON.parse(JSON.stringify(depts)))
      this.depts = depts
      this.departs = tranListToTreeData(depts, '')
      this.loading = false
    },
    closeDialogFn(flag) {
      this.dialogVisible = flag
    },
    // 显示添加弹窗
    handleAddDepts(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
    },
    // 显示编辑弹窗
    handeleEditDepts(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
      const addDeptRef = this.$refs.addDeptRef
      // console.log('666', addDeptRef)
      addDeptRef.getEmployeeSimple()
      // 获取详情
      addDeptRef.getDepartDetail(nodeData.id)
    }
  }

}
</script>
<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
